import 'package:flutter/material.dart';
import 'package:flutter_worker730/home/Home_page.dart';
import 'package:flutter_worker730/mine/Mine_page.dart';
import 'package:flutter_worker730/utils/EventBus.dart';
import 'package:flutter_worker730/utils/TokenManager.dart';

class TabsPage extends StatefulWidget {
  const TabsPage({Key? key}) : super(key: key);

  @override
  _TabsPageState createState() => _TabsPageState();
}

class _TabsPageState extends State<TabsPage> {
  List tabsList = [
    {
      'label': '首页',
      'icon': '/tabs/home_default.png',
      'activeIcon': '/tabs/home_active.png'
    },
    {
      'label': '我的',
      'icon': '/tabs/my_default.png',
      'activeIcon': '/tabs/my_active.png'
    }
  ]; //底部导航栏数据
  @override
  void initState() {
    super.initState();
    // 初始化token
    // tokenManager.initToken();

    eventBus.on<LogoutEvent>().listen((event) {
      print('我该登录了...');
      Navigator.pushNamed(context, '/login');
    });
  }

  void setActiveIndex(int index) {
    activeIndex = index;
    setState(() {});
  }

  //默认选中的索引
  int activeIndex = 0;

  List<BottomNavigationBarItem> getTabsBar() {
    List<BottomNavigationBarItem> list = [];
    for (var i = 0; i < tabsList.length; i++) {
      list.add(
        BottomNavigationBarItem(
            label: tabsList[i]["label"],
            icon: Image.asset(
              tabsList[i]["icon"],
              width: 30,
              height: 30,
            ),
            activeIcon: Image.asset(
              tabsList[i]["activeIcon"],
              width: 30,
              height: 30,
            )),
      );
    }
    return list;
  } // 创建底部导航栏

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: IndexedStack(
          index: activeIndex,
          children: [
            HomePage(),
            MinePage(activeIndex: activeIndex, setActiveIndex: setActiveIndex),
          ],
        ),
        bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            currentIndex: activeIndex,
            selectedItemColor: const Color.fromARGB(255, 85, 145, 175),
            unselectedItemColor: Colors.black,
            items: getTabsBar(),
            onTap: (int index) {
              setState(() {
                activeIndex = index;
              });
            }));
  }
}
